<template>
	<view class="status_bar">
		<!-- 这里是状态栏 -->
	</view>
	<view class="page-nav-bar">
		<view class="page-nav-bar-left">
			<view class="avatar">
				
			</view>
			<text class="user-name">{{ userInfo.name }}</text>
		</view>
		<view class="page-nav-bar-right">
			
		</view>
	</view>
	<view class="page-search">
		<uni-search-bar radius="5" placeholder="搜索..." cancelButton="none"/>
	</view>
	<view class="page-search">
		<uni-list>
			<uni-list-item clickable show-extra-icon
				 :extra-icon="{color: '#555555',size: '30',type: 'person'}" v-for="conv of convList" :key="conv._id" :title="conv.title" :note="conv.lastTime" @click="toChat(conv)"></uni-list-item>
		</uni-list>
	</view>

</template>

<script setup lang="ts">
	import { ref, onMounted } from 'vue';
	import request from '../../common/request';
	const userInfo = uni.getStorageSync('userInfo')
	
	const convList = ref([])
	async function getConvList() {
		try{
			const { code, data } = await request.post('/convList')
			// console.log('/convList', data)
			if (code) {
				convList.value = data.map((item: any) => {
					if (item.crowd.length === 2) {
						// 私聊
						let u = item.crowd.find((it: any) => it._id != userInfo._id)
						item.title = u.name
					}
					return item
				})
			}
		}catch(e){
			//TODO handle the exception
		}
	}
	
	onMounted(() => {
		getConvList()
	})
	
	const toChat = async (conv) => {
		uni.setStorageSync('chat-conv', conv)
		uni.navigateTo({
			url: '/pages/chat/chat',
			animationType: 'pop-in',
			animationDuration: 200
		})
	}
</script>

<style lang="scss">
	
</style>
